home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / mtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  4.8 KB  |  207 lines

  1. #include <alloca.h>
  2. #include <ctype.h>
  3. #include <linux/fs.h>
  4. #include <newt.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <sys/mount.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11.  
  12. #include "devices.h"
  13. #include "fs.h"
  14. #include "install.h"
  15. #include "log.h"
  16. #include "mkswap.h"
  17. #include "perror.h"
  18. #include "run.h"
  19. #include "windows.h"
  20.  
  21. static int partFilter(struct partition * part) {
  22.     int rc;
  23.  
  24.     if (doMount(part->device, "/mnt", "ext2", 0, 0)) {
  25.     return 0;
  26.     }
  27.  
  28.     if (access("/mnt/etc", R_OK) || access("/mnt/etc/fstab", R_OK) ||
  29.     access("/mnt/proc", R_OK) || access("/mnt/bin", R_OK) ||
  30.     access("/mnt/etc/redhat-release", R_OK)) {
  31.     rc = 0;
  32.     } else 
  33.     rc = 1;
  34.  
  35.     umount("/mnt");
  36.  
  37.     return rc;
  38. }
  39.  
  40. int readMountTable(struct partitionTable table, struct fstab * finalFstab) {
  41.     newtComponent okay, cancel, form, text, listbox, answer;
  42.     int rootPartNum;
  43.     struct partition * rootPart = NULL;
  44.     FILE * f;
  45.     char buf[200];
  46.     char * start, * end;
  47.     char device[50], mntpoint[50], type[50];
  48.     struct fstab fstab;
  49.     struct fstabEntry entry;
  50.     int numParts;
  51.  
  52.     umountFilesystems(finalFstab);
  53.  
  54.     newtOpenWindow(10, 2, 55, 19, "Root Partition");
  55.     text = newtTextbox(1, 1, 53, 2, NEWT_TEXTBOX_WRAP);
  56.     newtTextboxSetText(text, "What partition holds the root partition "
  57.                "of your installation?");
  58.  
  59.     okay = newtButton(11, 15, "Ok");
  60.     cancel = newtButton(33, 15, "Cancel");
  61.  
  62.     form = newtForm(NULL, NULL, 0);
  63.     listbox = addPartitionListbox(table, form, 3, 4, 7, PART_EXT2,
  64.                   partFilter, &numParts);
  65.  
  66.     if (!numParts) {
  67.     newtFormDestroy(form);
  68.  
  69.     /* try again, but don't be quite so picky */
  70.     form = newtForm(NULL, NULL, 0);
  71.     listbox = addPartitionListbox(table, form, 3, 4, 7, PART_EXT2,
  72.                       NULL, &numParts);
  73.     if (!numParts) {
  74.         newtFormDestroy(form);
  75.         newtPopWindow();
  76.  
  77.         errorWindow("You don't have any Linux partitions. You "
  78.                 "can't upgrade this system!");
  79.         return INST_CANCEL;        /* INST_ERROR would hang everything */
  80.     }
  81.     }
  82.  
  83.     if (numParts == 1) {
  84.     rootPart = newtListboxGetCurrent(listbox);
  85.     rootPartNum = (rootPart - table.parts);
  86.     logMessage("Using partition %s for the root device",
  87.             table.parts[rootPartNum].device);
  88.     if (doMount(table.parts[rootPartNum].device, "/mnt", "ext2", 0, 0)) {
  89.         errorWindow("Could not mount automatically selected device.");
  90.         answer = NULL;
  91.         return INST_ERROR;
  92.     }
  93.     answer = okay;
  94.     } else {
  95.     newtFormAddComponents(form, text, okay, cancel, NULL);
  96.  
  97.     do {
  98.         answer = newtRunForm(form);
  99.         if (answer == cancel) continue;
  100.  
  101.         rootPart = newtListboxGetCurrent(listbox);
  102.         rootPartNum = (rootPart - table.parts);
  103.  
  104.         if (doMount(table.parts[rootPartNum].device, "/mnt", "ext2", 
  105.             0, 0)) {
  106.         errorWindow("Could not mount device.");
  107.         answer = NULL;
  108.         continue;
  109.         }
  110.  
  111.         if (testing) {
  112.         continue;
  113.         }
  114.  
  115.         if (access("/mnt/etc", R_OK) || access("/mnt/etc/fstab", R_OK) ||
  116.         access("/mnt/proc", R_OK) || access("/mnt/bin", R_OK)) {
  117.         errorWindow("That doesn't appear to be a root partition.");
  118.         umount("/mnt");
  119.         answer = NULL;
  120.         }
  121.     } while (!answer);
  122.     }
  123.             
  124.     newtFormDestroy(form);
  125.     newtPopWindow();
  126.  
  127.     if (answer == cancel) return INST_CANCEL;
  128.  
  129.     if (testing) 
  130.     f = fopen("/etc/fstab", "r");
  131.     else
  132.     f = fopen("/mnt/etc/fstab", "r");
  133.  
  134.     if (!f) {
  135.     errorWindow("Cannot read /mnt/etc/fstab: %s");
  136.     return INST_ERROR;
  137.     }
  138.  
  139.     memset(&fstab, 0, sizeof(fstab));
  140.  
  141.     while (fgets(buf, sizeof(buf) - 1, f)) {
  142.     start = buf;
  143.     while (*start && isspace(*start)) start++;
  144.     if (!*start || *start == '#') continue;
  145.  
  146.     if (strncmp(start, "/dev/", 5)) {
  147.         continue;
  148.     }
  149.  
  150.     if (strstr(start, "noauto")) continue;
  151.  
  152.     end = start + strlen(start) - 1;
  153.     while (isspace(*end)) end--;
  154.     end++;
  155.     *end = '\0';
  156.  
  157.     if (sscanf(start, "%s %s %s", device, mntpoint, type) != 3) {
  158.         errorWindow("Bad line in /mnt/etc/fstab -- aborting");
  159.         fclose(f);
  160.         freeFstab(fstab);
  161.         umount("/mnt");
  162.         return INST_ERROR;
  163.     }
  164.  
  165.     if (strncmp(device, "/dev/hd", 7) && strncmp(device, "/dev/sd", 7))
  166.         continue;
  167.  
  168.     entry.device = strdup(device + 5);
  169.     entry.size = 0;
  170.     entry.isMounted = 0;
  171.     entry.doFormat = 0;
  172.     entry.mntpoint = strdup(mntpoint);
  173.  
  174.     if (!strcmp(type, "ext2")) {
  175.         entry.type = PART_EXT2;
  176.         entry.tagName = "Linux native";
  177.     } else if (!strcmp(type, "swap")) {
  178.         entry.type = PART_SWAP;
  179.         entry.tagName = "Linux swap";
  180.         if (canEnableSwap) {
  181.         enableswap(entry.device, 0, 0);
  182.         canEnableSwap = 0;
  183.         }
  184.     } else if (!strcmp(type, "msdos")) {
  185.         entry.type = PART_DOS;
  186.         entry.tagName = "DOS 16-bit >=32";
  187.     } else if (!strcmp(type, "hpfs")) {
  188.         entry.type = PART_HPFS;
  189.         entry.tagName = "OS/2 HPFS";
  190.     } else {
  191.         entry.type = PART_OTHER;
  192.         entry.tagName = "Unknown";
  193.     }
  194.  
  195.     addFstabEntry(&fstab, entry);
  196.     }
  197.  
  198.     fclose(f);
  199.  
  200.     umount("/mnt");
  201.  
  202.     freeFstab(*finalFstab);
  203.     *finalFstab = fstab;
  204.  
  205.     return 0;
  206. }
  207.